# -*- coding: utf-8 -*-
import   poplib   
import   cStringIO   
import   email   
import   base64
import   os
import   re

    
#POP3取信   
M   =   poplib.POP3('pop3.sina.com')   
M.user('chenlu5867')   
M.pass_('1982623')   
    
#打印有多少封信   
numMessages   =   len(M.list()[1])   
print   'num   of   messages',   numMessages   
    
    
for   i   in   range(numMessages):           
        m   =   M.retr(i+1)   
            
        buf   =   cStringIO.StringIO()   
        for   j   in   m[1]:                   
                print   >>buf,   j   
        buf.seek(0)   
    
          #解析信件内容   
        msg   =   email.message_from_file(buf)   
        for   part   in   msg.walk():   
                contenttype   =     part.get_content_type()     
                filename   =   part.get_filename()   
                #不知道什么原因MailDesktop发出的附件类型是‘text/base64’所以加上了 or contenttype   ==   'text/base64'
                if   filename and (contenttype   ==   'application/octet-stream' or contenttype   ==   'text/base64'):   
                        #   保存附件
                        #print  contenttype
                        f   =   open("%s"   %   (filename),'wb')   
                        f.write(base64.decodestring(part.get_payload()))   
                        f.close()
                        """
                elif   contenttype   ==   'text/plain':   
                        #   保存正文   
                        f   =   open("mail%d.txt"   %   (i+1),'wb')   
                        f.write(base64.decodestring(part.get_payload()))   
                        f.close()
                        """

zip_command='rename *.RAR_gm *.rar'
if os.system(zip_command)==0:
        print u'去除_gm后缀成功'
zip_command='rar e *.zip.part1.rar'
if os.system(zip_command)==0:
        print u'解压成功'
zip_command='del e *.zip.part*.rar'
if os.system(zip_command)==0:
        print u'清除成功'

    
                        
                
                
        
